package com.clp.protocol.iec104.apdu.asdu.info_obj.qua;

import com.clp.protocol.iec104.definition.Tm;
import io.netty.buffer.ByteBuf;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * 遥测限定词（即品质描述词）
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class TmQua extends Qua {
    private Tm.Valid valid;

    @Override
    public TmQua initBy(ByteBuf buf) {
        byte val = buf.readByte();
        this.valid = Tm.Valid.gain((val & 0x80)==0x80 ? 1 : 0);
        return this;
    }

    @Override
    public boolean isValid() {
        return valid != null;
    }

    @Override
    public void writeTo(ByteBuf buf) {
        byte by = 0x00;
        by |= valid.getVal() == 1 ? 0x80 : 0x00;
    }

    @Override
    public String toString() {
        return valid.getDesc() + "(" + valid.getVal() + ")";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TmQua tmQua = (TmQua) o;
        return valid == tmQua.valid;
    }

    @Override
    public int hashCode() {
        return Objects.hash(valid);
    }
}
